//
//  PublicContentViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/22.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "PublicContentViewController.h"
#import "PublicArticleViewController.h"
@interface PublicContentViewController ()<UIWebViewDelegate>
@property (nonatomic,strong)UIWebView * contentWebView;
@end

@implementation PublicContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_pageType == 1) {
        [self.view addSubview:self.contentWebView];
        if (![_billUrl containsString:@"https://"]) {
             [self.contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://%@",_billUrl]]]];
        }else{
          [self.contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_billUrl]]]];
        }
     
    }else{
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setImage:[UIImage imageNamed:@"icon- 点点"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(rightBtnDidClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn sizeToFit];
    rightBtn.frame = CGRectMake(0, 0, 44, 44);
    rightBtn.jk_touchAreaInsets  = UIEdgeInsetsMake(10, 10, 10, 10);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.gk_navRightBarButtonItem = rightItem;
    [self.view addSubview:self.contentWebView];
    NSString * url = [NSString stringWithFormat:@"%@?fuuid=%@&t=%@",_model.url,[LoginManager shareLoginManager].userModel.fuuid,_model.publicUdid];
   [self.contentWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
    }

    self.gk_navigationItem.hidesBackButton = YES;
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
      [backBtn setImage:[UIImage imageNamed:@"icon-返回键"] forState:UIControlStateNormal];
      [backBtn addTarget:self action:@selector(backacrion) forControlEvents:UIControlEventTouchUpInside];
      [backBtn sizeToFit];
      backBtn.frame = CGRectMake(0, 0, 44, 44);
      backBtn.jk_touchAreaInsets  = UIEdgeInsetsMake(10, 10, 10, 10);
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.gk_navLeftBarButtonItem = backItem;
}

- (void)backacrion{

if ([self.contentWebView canGoBack]) {

/// 网页可以返回 就进行网页返回

[self.contentWebView goBack];

}else{

[self.view resignFirstResponder];

/// 网页返回到首页了 返回不了了 这时候我们的控制器返回

[self.navigationController popViewControllerAnimated:YES];

}

}


-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self showAlwaysTipHudWithText:@"加载中，请稍等"];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self hideHud];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self showTipHudWithText:@"网络错误，稍后再试"];
}

-(void)rightBtnDidClickAction:(UIButton*)sender
{
    PublicArticleViewController * vc = [[PublicArticleViewController alloc]init];
    vc.pageType = 1;
    vc.publicUdid = _model.publicUdid;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UIWebView *)contentWebView{
    if (!_contentWebView) {
        _contentWebView = [[UIWebView alloc]initWithFrame:CGRectMake(0, self.gk_navigationBar.bottom, kScreenWidth, kScreenHeight-self.gk_navigationBar.bottom)];
        _contentWebView.delegate = self;
    }
    return _contentWebView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
